<?php
$db = new Database;
$auth = Auth::getAuth('current_user');
$user_id = $auth['id'];
$company_id = $auth['company_id'];
$user_groupType = $auth['role_type'];
$user_group = $auth['role'];
$userLevel = $auth['userlevel_id'];
if(isset($_POST['action'])){
    if($_POST['action']=="requestToApprove"){
        $getPosition = $db->query("SELECT * FROM tbposition WHERE id='".$auth['position_id']."' and company_id='$company_id' ","row");
        $numrowsRequest = $db->query("SELECT * FROM tbrequest r LEFT JOIN tbrequest_process rp on r.id=rp.request_id LEFT JOIN tbform f on f.id=r.form_id where (".roleApprover($user_groupType,$user_group)." or rp.approver='".$auth['firstname']." ".$auth['lastname'] ."' or rp.approver='".$getPosition['position']."') and rp.status=0 and r.status=0","numrows");
        $numrowsRegRequest = $db->query("SELECT * from tbuser where status = 0 and company_id = '$company_id' and is_active = 1","numrows");
        if($userLevel==1){
            echo $numrowsRegRequest+$numrowsRequest;
        }else{
            echo $numrowsRequest;
        }
        
    }else if($_POST['action']=="pendingRequest"){
        $numrowsRequest = $db->query("SELECT * FROM tbrequest r LEFT JOIN tbrequest_process rp on r.id=rp.request_id LEFT JOIN tbform f on f.id=r.form_id where r.user_id = '".$auth['id']."' and r.status = 0 and rp.status=0","numrows");
        echo $numrowsRequest;
    }else if($_POST['action']=="regUser"){
        
    }else if($_POST['action']=="userList"){
        $numrowsUserList = $db->query("SELECT * from tbuser where status = 1 and company_id = '$company_id' and is_active = 1","numrows");
        echo $numrowsUserList;
    }else if($_POST['action']=="deletedAccounts"){
        $numrowsDeleteAcct = $db->query("SELECT * from tbuser where company_id = '$company_id' and is_active = 0","numrows");
        echo $numrowsDeleteAcct;
    }else if($_POST['action']=="checkOrgChart"){
        $numrowsOrgchart = $db->query("SELECT * from tborgchart where company_id = '$company_id' and status = 1 and is_active = 1","numrows");
        echo $numrowsOrgchart;
    }else if($_POST['action']=="checkPositionRow"){
        $roleToInsert = $_POST['roleToInsert'];
        $roleTypeToInsert = $_POST['roleTypeToInsert'];
        if($roleTypeToInsert==1 || $roleTypeToInsert==2){
            $numrowsUserHead = $db->query("SELECT * from tbuser where role='$roleToInsert' and role_type='$roleTypeToInsert' and company_id='$company_id' and status = 1","numrows");
            if($numrowsUserHead==1){
                echo "doNotAccept";
            }
        }
    }else if($_POST['action']=="checkPositionRowMulti"){
        $roleToInsert = explode(",",implode(",",$_POST['roleToInsert']));
        $roleTypeToInsert = explode(",",implode(",",$_POST['roleTypeToInsert']));
        $errorCount = 0;
        for($a=0;$a<count($roleToInsert);$a++){
            $numrowsUserHead = $db->query("SELECT * from tbuser where role='$roleToInsert[$a]' and role_type='$roleTypeToInsert[$a]' and company_id='$company_id' and status = 1","numrows");
            if($numrowsUserHead==1){
                $errorCount++;
            }
        }
        if($errorCount>0){
            echo "doNotAccept";
        }
    }else if($_POST['action']=="checkPositionRowEdit"){
        $userID = $_POST['userID'];
        $roleToInsert = $_POST['roleToInsert'];
        $roleTypeToInsert = $_POST['roleTypeToInsert'];
        if($roleTypeToInsert==1 || $roleTypeToInsert==2){
            $numrowsUserHead = $db->query("SELECT * from tbuser where role='$roleToInsert' and role_type='$roleTypeToInsert' and company_id='$company_id' and status = 1 and id!=$userID","numrows");
            if($numrowsUserHead==1){
                echo "doNotAccept";
            }
        }
    }else if($_POST['action']=="countOrgchart"){
        $numrowsDeleteAcct = $db->query("SELECT * from tborgchart where company_id = '$company_id'","numrows");
        echo $numrowsDeleteAcct;
    }
    // for notification
    else if($_POST['action']=="messageNoti"){
        $message = $db->query("SELECT * FROM tbsend_message WHERE user_read='0'","array");
        foreach($message as $data){
            $split = explode(",", $data['m_recepient_id']);
            $mID = $data['id'];
            foreach($split as $msg){
                if($user_id==$msg){
                    $count = $db->query("SELECT * FROM tbsend_message WHERE id='$mID' AND user_read='0'","numrows");
                    echo $count;
                }
                
            }
        }
    }
    // For online user
    else if($_POST['action']=="online"){
        $userAvatarMessage = $db->query("SELECT * FROM tbuser WHERE online='1' and id!='$user_id' and company_id ='$company_id'","array");
        foreach($userAvatarMessage as $imageData){
            $name = $imageData['firstname'].'-'.$imageData['lastname'];
            echo "<a href=\"javascript:void(0)\" onclick=\"javascript:chatWith('".$imageData['firstname']."')\">";
            //echo "<a class='openChat'>";
        echo myAvatar($imageData['extension'],$imageData['id'],"30","30","border:1px solid #ddd;");
            echo "</a>";
            //echo '</a>';
        }
    }elseif($_POST['action']=="checkOnline"){ // Check if user is still online
        $userID = $_POST['userID'];
        $query = $db->query("SELECT * FROM tbuser WHERE id='$userID' AND online='1'","row");
        $queryNum = $db->query("SELECT * FROM tbuser WHERE id='$userID' AND online='1'","numrows");
        if($queryNum==0){
            echo "Not Online";
            $update = array("online"=>0);
            $set = array("id"=>$userID);
            $db->update("tbuser",$update,$set);
        }else{
            echo "Online";
        }
    }
}
?>